home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1999 April / Cd Pc Users extra 19 abril 1999.iso / Prog / Inst / Cipher / FORM1.FRM (.txt) next >
Encoding:
Visual Basic Form  |  1998-08-17  |  5.4 KB  |  183 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   2655
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1515
  7.    ClientWidth     =   8775
  8.    Height          =   3060
  9.    Left            =   1080
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   2655
  12.    ScaleWidth      =   8775
  13.    Top             =   1170
  14.    Width           =   8895
  15.    Begin VB.CommandButton cmdDecipher 
  16.       Caption         =   "<<"
  17.       Enabled         =   0   'False
  18.       Height          =   495
  19.       Left            =   3840
  20.       TabIndex        =   7
  21.       Top             =   1800
  22.       Width           =   1095
  23.    End
  24.    Begin VB.CommandButton cmdCipher 
  25.       Caption         =   ">>"
  26.       Enabled         =   0   'False
  27.       Height          =   495
  28.       Left            =   3840
  29.       TabIndex        =   6
  30.       Top             =   960
  31.       Width           =   1095
  32.    End
  33.    Begin VB.TextBox txtPassword 
  34.       Height          =   285
  35.       Left            =   3480
  36.       TabIndex        =   5
  37.       Top             =   360
  38.       Width           =   1815
  39.    End
  40.    Begin VB.TextBox txtCipher 
  41.       Height          =   2295
  42.       Left            =   5400
  43.       MultiLine       =   -1  'True
  44.       TabIndex        =   2
  45.       Top             =   0
  46.       Width           =   3375
  47.    End
  48.    Begin VB.TextBox txtPlain 
  49.       Height          =   2295
  50.       Left            =   0
  51.       MultiLine       =   -1  'True
  52.       TabIndex        =   0
  53.       Text            =   "Form1.frx":0000
  54.       Top             =   0
  55.       Width           =   3375
  56.    End
  57.    Begin VB.Label Label1 
  58.       Alignment       =   2  'Center
  59.       Caption         =   "Password"
  60.       Height          =   255
  61.       Index           =   2
  62.       Left            =   3480
  63.       TabIndex        =   4
  64.       Top             =   120
  65.       Width           =   1815
  66.    End
  67.    Begin VB.Label Label1 
  68.       Alignment       =   2  'Center
  69.       Caption         =   "Ciphertext"
  70.       Height          =   255
  71.       Index           =   1
  72.       Left            =   5400
  73.       TabIndex        =   3
  74.       Top             =   2400
  75.       Width           =   3375
  76.    End
  77.    Begin VB.Label Label1 
  78.       Alignment       =   2  'Center
  79.       Caption         =   "Plaintext"
  80.       Height          =   255
  81.       Index           =   0
  82.       Left            =   0
  83.       TabIndex        =   1
  84.       Top             =   2400
  85.       Width           =   3375
  86.    End
  87. Attribute VB_Name = "Form1"
  88. Attribute VB_Creatable = False
  89. Attribute VB_Exposed = False
  90. Option Explicit
  91. ' Encipher the text using the pasword.
  92. Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String)
  93. Const MIN_ASC = 32  ' Space.
  94. Const MAX_ASC = 126 ' ~.
  95. Const NUM_ASC = MAX_ASC - MIN_ASC + 1
  96. Dim offset As Long
  97. Dim str_len As Integer
  98. Dim i As Integer
  99. Dim ch As Integer
  100.     ' Initialize the random number generator.
  101.     offset = NumericPassword(password)
  102.     Rnd -1
  103.     Randomize offset
  104.     ' Encipher the string.
  105.     str_len = Len(from_text)
  106.     For i = 1 To str_len
  107.         ch = Asc(Mid$(from_text, i, 1))
  108.         If ch >= MIN_ASC And ch <= MAX_ASC Then
  109.             ch = ch - MIN_ASC
  110.             offset = Int((NUM_ASC + 1) * Rnd)
  111.             ch = ((ch + offset) Mod NUM_ASC)
  112.             ch = ch + MIN_ASC
  113.             to_text = to_text & Chr$(ch)
  114.         End If
  115.     Next i
  116. End Sub
  117. ' Encipher the text using the pasword.
  118. Private Sub Decipher(ByVal password As String, ByVal from_text As String, to_text As String)
  119. Const MIN_ASC = 32  ' Space.
  120. Const MAX_ASC = 126 ' ~.
  121. Const NUM_ASC = MAX_ASC - MIN_ASC + 1
  122. Dim offset As Long
  123. Dim str_len As Integer
  124. Dim i As Integer
  125. Dim ch As Integer
  126.     ' Initialize the random number generator.
  127.     offset = NumericPassword(password)
  128.     Rnd -1
  129.     Randomize offset
  130.     ' Encipher the string.
  131.     str_len = Len(from_text)
  132.     For i = 1 To str_len
  133.         ch = Asc(Mid$(from_text, i, 1))
  134.         If ch >= MIN_ASC And ch <= MAX_ASC Then
  135.             ch = ch - MIN_ASC
  136.             offset = Int((NUM_ASC + 1) * Rnd)
  137.             ch = ((ch - offset) Mod NUM_ASC)
  138.             If ch < 0 Then ch = ch + NUM_ASC
  139.             ch = ch + MIN_ASC
  140.             to_text = to_text & Chr$(ch)
  141.         End If
  142.     Next i
  143. End Sub
  144. ' Translate a password into an offset value.
  145. Private Function NumericPassword(ByVal password As String) As Long
  146. Dim value As Long
  147. Dim ch As Long
  148. Dim shift1 As Long
  149. Dim shift2 As Long
  150. Dim i As Integer
  151. Dim str_len As Integer
  152.     str_len = Len(password)
  153.     For i = 1 To str_len
  154.         ' Add the next letter.
  155.         ch = Asc(Mid$(password, i, 1))
  156.         value = value Xor (ch * 2 ^ shift1)
  157.         value = value Xor (ch * 2 ^ shift2)
  158.         ' Change the shift offsets.
  159.         shift1 = (shift1 + 7) Mod 19
  160.         shift2 = (shift2 + 13) Mod 23
  161.     Next i
  162.     NumericPassword = value
  163. End Function
  164. Private Sub cmdCipher_Click()
  165. Dim cipher_text As String
  166.     Cipher txtPassword.Text, txtPlain.Text, cipher_text
  167.     txtCipher.Text = cipher_text
  168. End Sub
  169. Private Sub cmdDecipher_Click()
  170. Dim plain_text As String
  171.     Decipher txtPassword.Text, txtCipher.Text, plain_text
  172.     txtPlain.Text = plain_text
  173. End Sub
  174. Private Sub txtPassword_Change()
  175.     If Len(txtPassword.Text) > 0 Then
  176.         cmdCipher.Enabled = True
  177.         cmdDecipher.Enabled = True
  178.     Else
  179.         cmdCipher.Enabled = False
  180.         cmdDecipher.Enabled = False
  181.     End If
  182. End Sub
  183.